#!/bin/bash

# Curve Performance Comparison Script
# Compares BLS12-381 vs secp256k1 performance

echo "=== ADKR Curve Performance Comparison ==="
echo ""

# Build the benchmark tool
echo "Building curve benchmark tool..."
cd /home/Dory-BFT-Consensus/adkr
go build -o curve_bench ./cmd/curve_bench

if [ $? -ne 0 ]; then
    echo "Failed to build curve benchmark tool"
    exit 1
fi

echo "Benchmark tool built successfully"
echo ""

# Run BLS12-381 benchmark
echo "=== BLS12-381 Performance ==="
BLS_RESULT=$(./curve_bench bls12-381)
echo "$BLS_RESULT"
echo ""

# Run secp256k1 benchmark
echo "=== Secp256k1 Performance ==="
SECP_RESULT=$(./curve_bench secp256k1)
echo "$SECP_RESULT"
echo ""

# Parse and compare results
echo "=== Performance Comparison Summary ==="

# Extract key generation times
BLS_GEN=$(echo "$BLS_RESULT" | jq -r '.[0].avg_per_op_ms')
SECP_GEN=$(echo "$SECP_RESULT" | jq -r '.[0].avg_per_op_ms')

# Extract signing times
BLS_SIGN=$(echo "$BLS_RESULT" | jq -r '.[1].avg_per_op_ms')
SECP_SIGN=$(echo "$SECP_RESULT" | jq -r '.[1].avg_per_op_ms')

# Extract verification times
BLS_VERIFY=$(echo "$BLS_RESULT" | jq -r '.[2].avg_per_op_ms')
SECP_VERIFY=$(echo "$SECP_RESULT" | jq -r '.[2].avg_per_op_ms')

echo "Key Generation:"
echo "  BLS12-381:  ${BLS_GEN} ms"
echo "  Secp256k1:  ${SECP_GEN} ms"
if (( $(echo "$SECP_GEN < $BLS_GEN" | bc -l) )); then
    SPEEDUP=$(echo "scale=2; $BLS_GEN / $SECP_GEN" | bc -l)
    echo "  Secp256k1 is ${SPEEDUP}x faster"
else
    SLOWDOWN=$(echo "scale=2; $SECP_GEN / $BLS_GEN" | bc -l)
    echo "  BLS12-381 is ${SLOWDOWN}x faster"
fi
echo ""

echo "Signing:"
echo "  BLS12-381:  ${BLS_SIGN} ms"
echo "  Secp256k1:  ${SECP_SIGN} ms"
if (( $(echo "$SECP_SIGN < $BLS_SIGN" | bc -l) )); then
    SPEEDUP=$(echo "scale=2; $BLS_SIGN / $SECP_SIGN" | bc -l)
    echo "  Secp256k1 is ${SPEEDUP}x faster"
else
    SLOWDOWN=$(echo "scale=2; $SECP_SIGN / $BLS_SIGN" | bc -l)
    echo "  BLS12-381 is ${SLOWDOWN}x faster"
fi
echo ""

echo "Verification:"
echo "  BLS12-381:  ${BLS_VERIFY} ms"
echo "  Secp256k1:  ${SECP_VERIFY} ms"
if (( $(echo "$SECP_VERIFY < $BLS_VERIFY" | bc -l) )); then
    SPEEDUP=$(echo "scale=2; $BLS_VERIFY / $SECP_VERIFY" | bc -l)
    echo "  Secp256k1 is ${SPEEDUP}x faster"
else
    SLOWDOWN=$(echo "scale=2; $SECP_VERIFY / $BLS_VERIFY" | bc -l)
    echo "  BLS12-381 is ${SLOWDOWN}x faster"
fi
echo ""

# Calculate overall performance improvement
BLS_TOTAL=$(echo "$BLS_GEN + $BLS_SIGN + $BLS_VERIFY" | bc -l)
SECP_TOTAL=$(echo "$SECP_GEN + $SECP_SIGN + $SECP_VERIFY" | bc -l)

echo "Overall Performance:"
echo "  BLS12-381 Total:  ${BLS_TOTAL} ms"
echo "  Secp256k1 Total:  ${SECP_TOTAL} ms"
if (( $(echo "$SECP_TOTAL < $BLS_TOTAL" | bc -l) )); then
    SPEEDUP=$(echo "scale=2; $BLS_TOTAL / $SECP_TOTAL" | bc -l)
    echo "  Secp256k1 is ${SPEEDUP}x faster overall"
else
    SLOWDOWN=$(echo "scale=2; $SECP_TOTAL / $BLS_TOTAL" | bc -l)
    echo "  BLS12-381 is ${SLOWDOWN}x faster overall"
fi
echo ""

echo "=== Recommendation ==="
if (( $(echo "$SECP_TOTAL < $BLS_TOTAL" | bc -l) )); then
    echo "✅ Use secp256k1 for better performance"
    echo "   Set environment variable: ADKR_CURVE=secp256k1"
else
    echo "✅ Use BLS12-381 for better performance"
    echo "   Set environment variable: ADKR_CURVE=bls12-381"
fi
echo ""

# Clean up
rm -f curve_bench
echo "Benchmark completed successfully!"



